package com.heima.wemedia.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.contants.WmNewsConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dto.NewsAuthDto;
import com.heima.model.wemedia.dto.WmNewsDownOrUpDto;
import com.heima.model.wemedia.dto.WmNewsDto;
import com.heima.model.wemedia.dto.WmNewsPageReqDto;
import com.heima.model.wemedia.pojo.WmNews;
import com.heima.model.wemedia.vo.WmNewsVo;
import com.heima.wemedia.service.WmNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
@Api(value = "自媒体文章管理", tags = "自媒体文章管理")
public class WmNewsController {

    @Resource
    private WmNewsService wmNewsService;

    @ApiOperation("自媒体文章发布")
    @PostMapping(path = "/submit")
    public ResponseResult<String> submit(@RequestBody WmNewsDto dto) {
        wmNewsService.submit(dto);
        return ResponseResult.success();
    }

    @PostMapping(path = "/list")
    @ApiOperation("自媒体文章列表查询")
    public ResponseResult<List<WmNews>> list(@RequestBody WmNewsPageReqDto dto) {
        Page<WmNews> page = wmNewsService.queryList(dto);
        return PageResponseResult.success(page);
    }

    @GetMapping(path = "/one/{id}")
    @ApiOperation("根据ID查询文章详情")
    public ResponseResult<WmNews> getById(@PathVariable("id") Long newsId) {
        WmNews wmNews = wmNewsService.getById(newsId);
        return ResponseResult.success(wmNews);
    }

    @PostMapping(path = "/down_or_up")
    @ApiOperation("文章上下架")
    public ResponseResult<String> downOrUp(@RequestBody WmNewsDownOrUpDto dto) {
        wmNewsService.downOrUp(dto);
        return ResponseResult.success();
    }

    @PostMapping("/list_vo")
    @ApiOperation("查询文章列表")
    public ResponseResult findList(@RequestBody NewsAuthDto dto) {
        Page<WmNewsVo> page = wmNewsService.findList(dto);
        return PageResponseResult.success(page);
    }

    @GetMapping("/one_vo/{id}")
    @ApiOperation("查询自媒体文章详情")
    public ResponseResult findWmNewsVo(@PathVariable("id") Integer id) {
        WmNewsVo wmNewsVo = wmNewsService.findWmNewsVo(id);
        return ResponseResult.success(wmNewsVo);
    }

    @PostMapping("/auth_pass")
    @ApiOperation("审核通过")
    public ResponseResult authPass(@RequestBody NewsAuthDto dto) {
        wmNewsService.updateStatus(WmNewsConstants.WM_NEWS_AUTH_PASS, dto);
        return ResponseResult.success();
    }

    @PostMapping("/auth_fail")
    @ApiOperation("审核失败")
    public ResponseResult authFail(@RequestBody NewsAuthDto dto) {
        wmNewsService.updateStatus(WmNewsConstants.WM_NEWS_AUTH_FAIL, dto);
        return ResponseResult.success();
    }
}

